import { formatDates } from '../common/data1.js'

function jointPic(value) {
  if (value) {
    return `https://www.yaozhuasocial.com/imgs/${value}`
  }
}

function formatDate(time) {
  // time = time * 1000;
  let date = new Date(time)
  return formatDates(date, 'yyyy-MM-dd hh:mm:ss')
}
function formatTime(time) {
  // time = time * 1000;
  let date = new Date(time)
  return formatDates(date, 'yyyy-MM-dd hh:mm:ss')
}

function countdown(time) {
  let date = Math.floor((time - new Date().getTime()) / 1000)
  return date
}

function retainTailNumber(value) {
  if (value.length > 4) {
    return value.substr(value.length - 4)
  } else {
    return value
  }
}
// 超出文字中间显示省略符
function ellipsis(value) {
  let len = value.length
  if (!value) return ''
  if (value.length > 10) {
    return value.substring(0, 3) + '...' + value.substring(len - 4, len)
  }
  return value
}
// 超出文字中间显示省略符
function ellipsis1(value) {
  let len = value.length
  if (!value) return ''
  if (value.length > 10) {
    return value.substring(0, 7) + '..' + value.substring(len - 0, len)
  }
  return value
}
// 超出文字中间显示省略符
function ellipsis2(value) {
  let len = value.length
  if (!value) return ''
  if (value.length > 10) {
    return value.substring(0, 4) + '****' + value.substring(len - 4, len)
  }
  return value
}

function number_format(number, decimals, dec_point, thousands_sep) {
  decimals = 2 //这里默认设置保留两位小数，也可以注释这句采用传入的参数
  /*
   * 参数说明：
   * number：要格式化的数字
   * decimals：保留几位小数
   * dec_point：小数点符号
   * thousands_sep：千分位符号
   * */
  number = (number + '').replace(/[^0-9+-Ee.]/g, '')
  var n = !isFinite(+number) ? 0 : +number,
    prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
    sep = typeof thousands_sep === 'undefined' ? ',' : thousands_sep,
    dec = typeof dec_point === 'undefined' ? '.' : dec_point
  var s = n.toString().split('.')
  var re = /(-?\d+)(\d{3})/
  while (re.test(s[0])) {
    s[0] = s[0].replace(re, '$1' + sep + '$2')
  }
  if ((s[1] || '').length < prec) {
    s[1] = s[1] || ''
    s[1] += new Array(prec - s[1].length + 1).join('0')
  } else {
    s[1] = s[1].substring(0, prec) //小数点位数超出长度时截取前面的位数
  }
  return s.join(dec)
}

export default {
  jointPic,
  formatDate,
  formatTime,
  retainTailNumber,
  ellipsis,
  ellipsis1,
  number_format,
  countdown,
  jointPic,
  ellipsis2,
}
